package net.expectx.pay.gateway.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import net.expectx.common.single.base.BaseResultConstants;
import net.expectx.common.single.util.Md5Util;
import net.expectx.pay.gateway.common.AdminResult;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author lijian
 */
public class ExpectXUtil {
    /**
     * 过滤语言
     * @param content
     * @param lanuage
     * @return
     */
    public static String filterLanguage(String content,String lanuage){

        try {
            JSONObject json=JSONObject.parseObject(content);
            return json.getString(lanuage);
        }catch (Exception e){
            return content;
        }
    }
    public static List<Long> toList(String val, String split){
        List<Long> longs=new ArrayList<>();
        String [] v=val.split(split);
        for (String s:v){
            longs.add(Long.valueOf(s));
        }
        return longs;
    }
    public static String putPairsSequenceAndTogether(Map<String, String> info,String key) {
        List<Map.Entry<String, String>> infoIds = new ArrayList<Map.Entry<String, String>>(info.entrySet());
        Collections.sort(infoIds, new Comparator<Map.Entry<String, String>>() {
            @Override
            public int compare(Map.Entry<String, String> arg0, Map.Entry<String, String> arg1) {
                return (arg0.getKey()).compareTo(arg1.getKey());
            }
        });
        String ret = "";
        for (Map.Entry<String, String> entry : infoIds) {
            ret += entry.getKey();
            ret += "=";
            ret += entry.getValue();
            ret += "&";
        }
        ret = ret.substring(0, ret.length() - 1);
        return ret;
    }

    public static AdminResult encrypt(HttpServletRequest request) {
        String encrypt = (request.getHeader("Encrypt"));
        Byte encryptionTime = 1;
        String encryptionCode = "ExpectX";
        /**
         * 判断是否有参数传入
         */
        Enumeration<String> enumeration = request.getParameterNames();
        if (enumeration.hasMoreElements()) {
            /**
             * 获得参数并字典排序
             */
            List<String> parameterValueList = new LinkedList<>();
            while (enumeration.hasMoreElements()) {
                String parameterName = enumeration.nextElement();
                if (!"Encrypt".equals(parameterName)){
                    parameterValueList.add(request.getParameter(parameterName));
                }
            }
            parameterValueList.add(encryptionCode);
            String[] parameterValues = new String[parameterValueList.size()];
            for (int i = 0, j = parameterValueList.size(); i < j; i++) {
                parameterValues[i] = parameterValueList.get(i);
            }
            System.out.println("前sort数组:" + JSON.toJSONString(parameterValues));
            Arrays.sort(parameterValues);
            StringBuffer parameterValue = new StringBuffer();
            for (int i = 0; i < parameterValues.length; i++) {
                parameterValue.append(parameterValues[i]);
            }
            System.out.println("后sort数组:" + JSON.toJSONString(parameterValues));
            System.out.println("排序后字符串:" + parameterValue.toString());
            System.out.println("排序后长度:" + parameterValue.toString().length());
            System.out.println("加密次数:" + encryptionTime);
            String tempEncrypt = Md5Util.md5(parameterValue.toString(), "UTF-8").toLowerCase();
            System.out.println("第0次加密:" + tempEncrypt);

            for (int i = 0; i < encryptionTime - 1; i++) {
                tempEncrypt = Md5Util.md5(tempEncrypt).toLowerCase();
                System.out.println("第" + (i + 1) + "次加密:" + tempEncrypt);
            }
            System.out.println("Encrypt String:" + tempEncrypt.toLowerCase());
            System.out.println("Receive Encrypt String:" + JSONObject.toJSONString(encrypt.toLowerCase()));
            if (!encrypt.toLowerCase().equals(tempEncrypt.toLowerCase())) {
                return new AdminResult(false, BaseResultConstants.PARAMETER_MANIPULATION, null);
            }

        }
        return new AdminResult(true, BaseResultConstants.SUCCESS, null);

    }
}
